// index.js
// // 获取应用实例
const app = getApp()
import goodsArr from "../../goodsArr"
Page({
  data: {
    realScroll: 0,//实时滚动监听
    allShare: [], //合租、整租
    tabindex: -1,
    tagid: -1,
    tagid1: -1,
    tagid2: -1,
    tagid3: -1,
    tagid4: -1,
    tagid5: -1,
    chooseArr: [], //选中集合
    tempArr: [],//临时选中集合
    filters: [], //筛选
    rents: [], //租金
    priceIndex: -1,
    sorts: [], //排序面板
    sortsIndex: -1,
    faindex: -1, // 位置面板
    chindex: -1,
    sonindex: -1,
    father: [],
    child: [],
    realSon: [],
    fix: false,
    CQlatitude: 36.91405,
    CQlongitude: 98.479113,
    screenH: 0, //当前设备屏幕高度
    scrollHeight: 0, //真实移动高度
    clickNumber: 1,
    navbarHeigt: 0,
    navbarTop: 0,
    elementTop: 0, //初始元素顶部的到窗口顶部距离
    clickTop: 0,//点击之后节点的顶部的距离
    four: [{
      imgurl: '../../static/house1.png',
      text: "自如友家"
    },
    {
      imgurl: '../../static/house2.png',
      text: "自如整租"
    },
    {
      imgurl: '../../static/house3.png',
      text: "自如寓"
    },
    {
      imgurl: '../../static/house4.png',
      text: "自如豪宅"
    },
    ],
    // 条件筛选
    conditions: [{
      "name": "位置",
      "myicon": "iconsanjiaoxing",
      "active": false
    },
    {
      "name": "合租/整租",
      "myicon": "iconsanjiaoxing",
      "active": false
    },
    {
      "name": "租金",
      "myicon": "iconsanjiaoxing",
      "active": false
    },
    {
      "name": "筛选",
      "myicon": "iconsanjiaoxing",
      "active": false
    },
    {
      "name": "1",
      "myicon": "iconpaixu",
      "active": false
    }
    ],
    //标签组
    targets: [{
      "name": "必看好房",
      check: 1
    },
    {
      "name": "最高满减1.2月租金",
      check: 2
    },
    {
      "name": "自如新舍",
      check: 3
    },
    {
      "name": "整租4.0",
      check: 4
    },
    {
      "name": "友家6.0",
      check: 5
    },
    {
      "name": "业主精选直租",
      check: 6
    },
    {
      "name": "深呼吸",
      check: 7
    }
    ],
    conindex: -1,
    show: false,
    cityIndex: -1,
    cityName: '',
    cityArr: [],
    stikeyJudge: false,
  },
  onLoad() {
    this.pageSetting()
    this.setData({
      roomsArr: goodsArr.orderInfo,
      cityArr: goodsArr.cities,
      locations: goodsArr.locations,
      rents: goodsArr.rents,
      sorts: goodsArr.sorts,
      allShare: goodsArr.allShare,
      filters: goodsArr.filters
    })
    this.findTop()
  },

  // 标签点击
  targetTap(e) {
    console.log(e)
    // 判断是否为可以多选的选项
    let idx = e.currentTarget.dataset.index

    if (e.currentTarget.dataset.item.id == 1001 || e.currentTarget.dataset.item.id == 2001 || e.currentTarget.dataset.item.id == 3001) {
      console.log(e)
      this.setData({
        tagid: e.currentTarget.dataset.index
      })
      this.data.tempArr.push(e.currentTarget.dataset.item.vals[idx])

    } else if (e.currentTarget.dataset.item.id == 2002 || e.currentTarget.dataset.item.id == 3002) {
      this.setData({
        tagid1: e.currentTarget.dataset.index
      })
      this.data.tempArr.push(e.currentTarget.dataset.item.vals[idx])
    } else if (e.currentTarget.dataset.item.id == 2003) {
      this.setData({
        tagid2: e.currentTarget.dataset.index
      })
      this.data.tempArr.push(e.currentTarget.dataset.item.vals[idx])
    }

    wx.setStorageSync('chooseArr', this.data.tempArr)

    console.log(this.data.tagid, this.data.tagid1, this.data.tagid2,)
    if (e.currentTarget.dataset.item.title == "房源特色") {
      console.log(e)
      this.setData({
        tagid3: e.currentTarget.dataset.index
      })
      this.data.tempArr.push(e.currentTarget.dataset.item.vals[idx])
    } else if (e.currentTarget.dataset.item.title == "签约时长") {
      console.log(e)
      this.setData({
        tagid4: e.currentTarget.dataset.index
      })
      this.data.tempArr.push(e.currentTarget.dataset.item.vals[idx])
    } else if (e.currentTarget.dataset.item.title == "房源状态") {
      console.log(e)
      this.setData({
        tagid5: e.currentTarget.dataset.index
      })
      this.data.tempArr.push(e.currentTarget.dataset.item.vals[idx])
    }
  },

  reset(e) {
    console.log(e)
    if (e.currentTarget.dataset.index === 0) {
      this.setData({
        tagid: -1,
        tagid1: -1,
        tagid2: -1,
        tempArr: []
      })
    } else {
      this.setData({
        tagid3: -1,
        tagid4: -1,
        tagid5: -1,
        tempArr: []
      })
    }
  },
  confirm(e) {
    console.log(e)

    this.setData({
      chooseArr: wx.getStorageSync('chooseArr'),
    })
    setTimeout(() => {
      if (this.data.chooseArr.length !== 0) {
        if (e.currentTarget.dataset.item == 3) {
          this.data.conditions[3].change = 1
        } else {
          this.data.conditions[1].change = 1
        }
        this.setData({
          conditions: this.data.conditions,
        })
      }
    }, 0);


    this.closePanel()
  },
  // tab切换
  tabClick(e) {
    console.log(e)
    // 清除选中标签
    if (e.currentTarget.dataset.index != this.data.tabindex) {
      this.setData({
        tabindex: e.currentTarget.dataset.index,
        skuArr: e.currentTarget.dataset.item.rentSku,
        tagid: -1,
        tagid1: -1,
        tagid2: -1,
      })
    }
  },
  handleDatas() {
    //  1分离数据
    let that = this
    this.data.father = this.data.locations.filter(f => f.pid == 0)
    this.data.child = this.data.locations.filter(c => c.pid !== 0)

  },

  rentChange(e) {
    console.log(e)
    this.data.conditions[2].name = e.currentTarget.dataset.item
    this.data.conditions[2].change = 1
    this.setData({
      conditions: this.data.conditions,
      priceIndex: e.currentTarget.dataset.index
    })
    // 300ms后关闭窗口
    setTimeout(() => {
      this.closePanel()
    }, 300);
  },

  sortChange(e) {
    console.log(e)
    // this.data.conditions[0].name = e.currentTarget.dataset.item
    this.data.conditions[4].change = 1
    this.setData({
      conditions: this.data.conditions,
      sortsIndex: e.currentTarget.dataset.index
    })
    // 300ms后关闭窗口
    setTimeout(() => {
      this.closePanel()
    }, 300);
  },
  // 三级触发
  sonTap(e) {
    console.log(e)
    this.setData({
      sonindex: e.currentTarget.dataset.index
    })
    if (this.data.realSon.length > 0) {
      this.data.conditions[0].name = e.currentTarget.dataset.item.name
      this.data.conditions[0].change = 1
      this.setData({
        conditions: this.data.conditions
      })
    }
    // 300ms后关闭窗口
    setTimeout(() => {
      this.closePanel()
    }, 300);
  },
  // 点击二级的时候
  childTap(e) {
    this.handleDatas()
    console.log(e)
    let tempSon = this.data.child.filter(c => c.pid == e.currentTarget.dataset.item.id)
    // 切换二级时改变当前样式，清空三级选中状态，改变三级数组
    if (this.data.chindex !== e.currentTarget.dataset.index) {
      this.setData({
        sonindex: -1,
        realSon: tempSon,
        chindex: e.currentTarget.dataset.index
      })
    }
  },
  // 点击一级的时候
  fatherTap(e) {
    this.handleDatas()
    console.log(this.data.father)
    console.log(e)

    let tempChild = this.data.child.filter(c => c.pid == e.currentTarget.dataset.item.id)
    e.currentTarget.dataset.item.children = tempChild
    // 如果选择附近，则改变过滤器的值,并关闭窗口
    if (e.currentTarget.dataset.item.children.length == 0) {
      this.data.conditions[0].name = e.currentTarget.dataset.item.name
      this.data.conditions[0].change = 1
      this.setData({
        conditions: this.data.conditions
      })
      // 300ms后关闭窗口
      setTimeout(() => {
        this.closePanel()
      }, 300);
    }
    // 切换一级时，清空二级选中状态、设置当前选中样式，三级，重置二级
    if (this.data.faindex !== e.currentTarget.dataset.index) {
      this.setData({
        chindex: -1,
        faindex: e.currentTarget.dataset.index,
        realIndexChild: tempChild,
        realSon: []
      })
    }

  },
  // 关闭遮罩层
  closePanel() {
    this.setData({
      ifshow: false,
      conindex: -1
    })
  },
  goCity() {
    this.setData({
      show: true
    })

    this.authorize();
  },
  goTop() {
    let that = this
    let query = wx.createSelectorQuery();
    query.select("#fixbox").boundingClientRect(
      function (rect) {
        console.log("fsdfsdf", that.data.elementTop)

        console.log("点击时节点的top", that.data.elementTop, rect.top)
        // 判断是否已经置顶
        if (that.data.clickTop !== rect.top) {
          that.setData({
            fix: true,
          })
        } else {
          // 如果滚动过，要判断两种情况：
          // 1.滚动到了顶部,fix始终为true 页面不滚动
          if (that.data.elementTop - that.data.screenH * 0.18 == 0) {
            that.setData({
              fix: true,
            })
            console.log("实际还要滚动的距离", that.data.scrollHeight)
          } else {
            that.setData({
              fix: true,
            })
            console.log("实际还要滚动的距离", that.data.scrollHeight)
          }
        }
        that.setData({
          clickTop: rect.top
        })
      }
    ).exec()
  },
  onPageScroll: function (e) {
    // console.log(e)
    // 判断是否置顶
    let that = this
    if (that.data.elementTop !== that.data.clickTop) {
      let scrollHeight = that.data.elementTop - that.data.screenH * 0.18

      let judgeRes = e.scrollTop > scrollHeight ? true : false
      if (that.data.fix === judgeRes) {
        return false
      }
      that.setData({
        fix: judgeRes
      })

    } else {
      let query = wx.createSelectorQuery();
      query.select("#fixbox").boundingClientRect(
        function (rect) {
          //1. 当滚动距离>距离固定距离要滚动的距离 就是fix
          //2. 需要滚动的距离如果大于0，就正常滑动，并且滑到顶部位置就固定
          //3. 判断节点是否固定了,判断节点被点击后的位置,固定了
          if (rect.top == that.data.clickTop) {
            let scrollHeight = rect.top - that.data.screenH * 0.18
            let judgeRes = e.scrollTop > scrollHeight ? true : false
            if (that.data.fix === judgeRes) {
              return false
            }
            that.setData({
              fix: judgeRes,
              clickTop: rect.top
            })
          }
        }
      ).exec()

    }


  },
  findTop() {
    let _that = this
    //创建一个节点查询器query，获取节点位置信息的查询请求
    let query = wx.createSelectorQuery();
    query.select("#fixbox").boundingClientRect(
      function (rect) {
        _that.setData({
          elementTop: rect.top
        })
      }
    ).exec()
  },
  // 去详情页
  goDetails(e) {
    wx.setStorage({
      data: e,
      key: 'roomItem',
    })
    wx.navigateTo({
      url: '../details/details?id=1',
    })
  },

  // 城市列表 选择城市
  choseCity(e) {
    this.close()
    this.setData({
      cityIndex: e.currentTarget.dataset.index,
      cityName: e.currentTarget.dataset.item.cityName
    })
  },


  // 获取位置信息
  authorize() {
    let that = this
    let sucres = {}
    // this.setData({
    //   // cityName: result.ad_info.city,
    //   cityIndex: 1,
    //   cityArr: that.data.cityArr
    // })

    wx.getLocation({
      type: 'wgs84',
      altitude: true,
      success(res) {
        // console.log(res)
        sucres = res
        let result = ''
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + sucres.latitude + ',' + sucres.longitude + '&key=6WGBZ-AHN62-3YYUN-C6RXA-7UHGT-FABLF&get_poi=0',
          success: (res) => {
            result = res.data.result
            console.log(res.data.result, result)
            that.data.cityArr[0].cityName = result.ad_info.city,
            that.setData({
              cityName: result.ad_info.city,
              cityIndex: 0,
              cityArr: that.data.cityArr
            })
          }
        })
      },
      fail(res) {
        that.setData({
          // cityName: result.ad_info.city,
          cityIndex: 1,
          cityArr: that.data.cityArr
        })
      }
    })

  },
  // 获取系统信息，设置导航栏上边距
  pageSetting() {
    // 设置
    let capsule = wx.getMenuButtonBoundingClientRect()
    // console.log(capsule)
    this.setData({
      navbarHeigt: capsule.height,
      navbarTop: capsule.top,
      navbarRight: capsule.right
    })
    wx.getSystemInfo({
      success: (res) => {
        console.log("系统信息", res)
        this.setData({
          screenH: res.screenHeight
        })
      },
    })
    console.log(this.data.navbarHeigt, this.data.navbarTop)
  },
  // 关闭页面
  close() {
    this.setData({
      show: false
    })
  },
  // 点击置顶、筛选器样式修改
  filterConditions(e) {
    console.log(this.data.chooseArr, e)
    this.goTop()
    this.setData({
      fix: true
    })

    if (this.data.conindex == e.currentTarget.dataset.index) {
      this.data.clickNumber += 1
      console.log(this.data.clickNumber)
    } else {
      this.data.clickNumber = 1
      // console.log(this.data.clickNumber)
    }

    if (this.data.clickNumber % 2 == 0) {
      // this.data.conindex == -1
      if (this.data.chooseArr.length === 0) {
        if (e.currentTarget.dataset.index == 1) {
          this.setData({
            tagid: -1,
            tagid1: -1,
            tagid2: -1,
            tempArr: []
          })
        } else {
          this.setData({
            tagid3: -1,
            tagid4: -1,
            tagid5: -1,
            tempArr: []
          })
        }

      }
      this.setData({
        conindex: -1,
        ifshow: false
      })

    } else {
      this.setData({
        conindex: e.currentTarget.dataset.index,
        ifshow: true
      })

      //默认设置tab
      if (e.currentTarget.dataset.index == 1) {
        this.setData({
          skuArr: this.data.allShare[0].rentSku,
          tabindex: 0,
        })
      }


    }

    // 设置一级
    if (this.data.conindex === 0) {
      this.data.father = this.data.locations.filter(f => f.pid == 0);
      this.setData({
        father: this.data.father,
        realIndexChild: [],
        realSon: []
      })
    }

  }
})